﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.cms
{
    public partial class News_List : System.Web.UI.Page
    {
        public static string mFilePath = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
            if (userInfo==null)
            {
                Response.Write(" <script> top.location.href='/cms/login.aspx'; </script> ");
            }
            AjaxPro.Utility.RegisterTypeForAjax(typeof(News_List));
            mFilePath = Server.MapPath("../../../uc/headPicture.ascx");
        }



        //装载商品表视图
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string BindTable()
        {
            var myWhere = "charindex('f',News_Flag)>0 and Client_Id<1000";
            DataTable dt = NewsInfoBll.GetNewsInfoTable(myWhere,"News_Id desc",  CommonConst.DB_Name);
            return JsonHelper.DataTableToJSON(dt);
        }

        //发布商品信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string ReleaseNews()
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                string repStart = ""; string repEnd = ""; string strName = "新闻图片";
                StringBuilder builder = new StringBuilder();
                var myWhere = "charindex('f',News_Flag)>0 and Client_Id<1000";
                DataTable dt = NewsInfoBll.GetNewsInfoTable(myWhere, "News_Id desc", CommonConst.DB_Name);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    builder.Append("<li>");
                    builder.Append("<a target=\"_blank\" href=\"/html/" + dt.Rows[i]["NC_Code"].ToString() + "/" + EncryptHelper.EncryptString(dt.Rows[i]["News_Id"].ToString()) + ".html\">");
                    builder.Append("<img src='"+dt.Rows[i]["News_Picture"].ToString()+"' width=\"240\" height=\"214\" /></a>");
                    builder.Append("<a target=\"_blank\" href=\"/html/" + dt.Rows[i]["NC_Code"].ToString() + "/" + EncryptHelper.EncryptString(dt.Rows[i]["News_Id"].ToString()) + ".html\" class=\"banner-lb-name\" title='" + dt.Rows[i]["News_Title"].ToString() + "'>" + CommonBll.CutString(dt.Rows[i]["News_Title"].ToString(), 30, false) + "</a>");
                    builder.Append("</li>");
                }
                repStart = "<%--start1--%>";
                repEnd = "<%--end1--%>";

                CommonBll.ReplaceFileContent(mFilePath, repStart, repEnd, builder.ToString());
                var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                dicHandleLog.Add("user_id", userInfo.Id);
                dicHandleLog.Add("computername", userInfo.Computername);
                dicHandleLog.Add("computerip", userInfo.Computerip);
                dicHandleLog.Add("handle_note", "发布" + strName + "到首页");
                dicHandleLog.Add("obj_guid", "News_Flag=f");
                LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                jsonResult.Add("msg", "发布成功！");
                jsonResult.Add("isreload", true);
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
